package com.seavenois.alife2;

/*************************************************/
/* Boxes are each one of the individual cells in */
/* the board. This class keep track of the data **/
/* the building placed in the box. ***************/
/*************************************************/
public class Box {
	//Absolute Coordinates, in reference to the (top, left) point of the background
	private int topX, topY, leftX, leftY, rightX, rightY, bottomX, bottomY;
	//Arrays to group coordinates
	private int[] top = new int[2];
	private int[] left = new int[2];
	private int[] right = new int[2];
	private int[] bottom = new int[2];
	//Coordinates for the drawable. It limites the area to draw the building built in the box
	public int drawX, drawY;
	//Coordinates for the drawable, array
	private int[] draw = new int[2];
	//Properties of the box
	private int building; 
	private boolean river;
	//Parameters of the box
	private int beds = 0, workplaces = 0, studyplaces = 0, cars = 0, inputCapacity = 0, consumption = 0, production = 0;
	double vibes = 0;
	private double underVibes = 0;
	private int[] hosting = {0, 0, 0, 0, 0, 0, 0};
	
	//The age of the box
	private int age;
	
	//Fill coordinate arrays with coordinate values
	private void populate_arrays(){
		top[0] = topX;
		top[1] = topY;
		left[0] = leftX;
		left[1] = leftY;
		right[0] = rightX;
		right[1] = rightY;
		bottom[0] = bottomX;
		bottom[1] = bottomY;
		draw[0] = drawX;
		draw[1] = drawY;		
	}
	
	//Constructors. Initialize the boxes by passing the four vertices coordinates
	public Box(int tx, int ty, int lx, int ly, int rx, int ry, int bx, int by){
		age = -1;
		topX = tx;
		topY = ty;
		leftX = lx;
		leftY = ly;
		rightX = rx;
		rightY = ry;
		bottomX = bx;
		bottomY = by;
		drawX = leftX;
		drawY = bottomY - Values.DRAWABLE_HEIGHT;
		populate_arrays();
		setRiver(false);
		setBuilding(Values.BUILDING_NONE);
	}
	
	public Box(int[] t, int[] l, int[] r, int[] b){
		age = -1;
		topX = t[0];
		topY = t[1];
		leftX = l[0];
		leftY = l[1];
		rightX = r[0];
		rightY = r[1];
		bottomX = b[0];
		bottomY = b[1];
		drawX = leftX;
		drawY = bottomY - Values.DRAWABLE_HEIGHT;
		populate_arrays();
		setRiver(false);
	}
	
	//Boolean to identify if coordinates are in the box
	public boolean coordinatesInBox(int a, int b, int box){
		//Check if (a, b) are in the absolute square
		if (a < leftX || a > rightX || b < topY || b > bottomY)
			return false;
		//First quadrant
		if (a > topX && b < rightY){
			if (b > (a - topX) * (rightY - topY) / (rightX - topX) + topY)
				return true;
			else
				return false;
		}
		//Second quadrant
		if (a < topX && b < rightY){
			if (b > (a - leftX) * (topY - leftY) / (topX - leftX) + leftY)
				return true;
			else
				return false;
		}
		//Third quadrant
		if (a < topX && b > rightY){
			if (b < (a - leftX) * (bottomY - leftY) / (bottomX - leftX) + leftY)
				return true;
			else
				return false;
		}
		//Fourth quadrant
		if (a > topX && b > rightY){
			if (b < (a - bottomX) * (rightY - bottomY) / (rightX - bottomX) + bottomY)
				return true;
			else
				return false;
		}
		//On the edges
		if (a == 0 || b == 0)
			return true;
		return false;
	}
	
	//Resets the box
	public void reset(){
		building = Values.BUILDING_NONE;
		beds = 0;
		workplaces = 0;
		studyplaces = 0;
		cars = 0;
		inputCapacity = 0;
		consumption = 0;
		production = 0;
		vibes = 0;
		for (int i = 0; i < 7; i ++)
			hosting[i] = 0;
	}
	
	//Setters and getters
	public void setRiver(boolean r){
		river = r;
	}
	
	public boolean isRiver(){
		return river;
	}
	
	public void setBuilding(int b){
		building = b;
	}
	
	public int getBuilding(){
		return building;
	}

	public int getAge() {
		return age;
	}
	
	public void setAge(int _age) {
		age = _age;
	}

	public void newYear() {
		if (age != -1)
			age ++;
	}
	
	public void resetAge() {
		this.age = 0;
	}
	
	public int getBeds() {
		return beds;
	}

	public void setBeds(int beds) {
		this.beds = beds;
	}

	public int getWorkplaces() {
		return workplaces;
	}

	public void setWorkplaces(int workplaces) {
		this.workplaces = workplaces;
	}

	public int getStudyplaces() {
		return studyplaces;
	}

	public void setStudyplaces(int studyplaces) {
		this.studyplaces = studyplaces;
	}

	public int getCars() {
		return cars;
	}

	public void setCars(int cars) {
		this.cars = cars;
	}

	public int getInputCapacity() {
		return inputCapacity;
	}

	public void setInputCapacity(int inputCapacity) {
		this.inputCapacity = inputCapacity;
	}

	public int getConsumption() {
		return consumption;
	}

	public void setConsumption(int consumption) {
		this.consumption = consumption;
	}
	
	public int getProduction() {
		return production;
	}

	public void setProduction(int production) {
		this.production = production;
	}

	public double getVibes() {
		return vibes;
	}

	public void setVibes(double vibes) {
		this.vibes = vibes;
	}
	
	public void addVibes(double d){
		this.underVibes = this.underVibes + d;
	}

	public double getUnderVibes() {
		return underVibes;
	}

	public void setUnderVibes(float underVibes) {
		this.underVibes = underVibes;
	}

	public int getHosting(int i) {
		return hosting[i];
	}

	public void setHosting(int i, int h) {
		this.hosting[i] = h;
	}
}
